const { parse } = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;

// 要解析的Vue组件代码
const code = `
export default {
  computed: {
    oParamsT() { // 短语法函数定义
      return { age: 999 }
    }
  },
  methods: {
    handleClick() {
      console.log('Button clicked');
    },
    getOutputModeInfo() {
      for (let i = 0; i < 100; i++) {
        if (i = 10) {
          console.log(i);
        } else {
          for (let indx = 0; indx < 10; indx++) {
            this.name = 555;
          }
        }
      }
    }
  }
}
`;

// 解析代码为AST
const ast = parse(code, {
  sourceType: 'module',
  plugins: ['objectRestSpread']
});

let computedValue = null;

// 遍历AST查找computed.oParamsT的返回值
traverse(ast, {
  ExportDefaultDeclaration(path) {
    const componentOptions = path.node.declaration;
    
    // 确保导出的是一个对象
    if (componentOptions.type === 'ObjectExpression') {
      // 查找computed属性
      const computedProp = componentOptions.properties.find(
        prop => prop.key.name === 'computed'
      );
      
      if (computedProp && computedProp.value.type === 'ObjectExpression') {
        // 查找oParamsT属性
        const oParamsTProp = computedProp.value.properties.find(
          prop => prop.key.name === 'oParamsT'
        );
        
        if (oParamsTProp) {
          // 处理ObjectMethod类型(简写方法)
          if (oParamsTProp.type === 'ObjectMethod') {
            const functionBody = oParamsTProp.body;
            
            // 查找返回语句
            const returnStatement = functionBody.body.find(
              stmt => stmt.type === 'ReturnStatement'
            );
            
            if (returnStatement) {
              computedValue = returnStatement.argument;
            }
          }
        }
      }
    }
  }
});

// 输出生成的代码
const result = computedValue 
  ? generate(computedValue).code 
  : '未找到返回值';

console.log('oParamsT的返回值:', result);
// 输出: { age: 999 }